home *** CD-ROM | disk | FTP | other *** search
- // layered.cpp : アプリケーション用のエントリ ポイントの定義
- //
-
- #include "stdafx.h"
- #include "layeredwnd.h"
-
- #define APPNAME "LayeredWindow"
- static char szAppName[] = APPNAME;
- static char szTitle[] = APPNAME;
-
- LayeredWnd *playeredWnd;
-
- HINSTANCE hInst; // USER32.DLL のインスタンスハンドル
- // SetLayeredWindowAttributes のアドレス
- BOOL (WINAPI *_SetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
-
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow )
- {
- // TODO: この位置にコードを記述してください。
- OSVERSIONINFO versioninfo;
- versioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- GetVersionEx( &versioninfo );
- if( versioninfo.dwMajorVersion<5 ){
- MessageBox( NULL, "このバージョンのWindowsでは動作しません。", "LayeredWindow",MB_OK|MB_ICONHAND );
- return FALSE;
- }
- if( !InitInstance( hInstance ) ){
- MessageBox( NULL, "アプリケーションの初期化に失敗しました。", "LayeredWindow", MB_OK|MB_ICONHAND );
- return FALSE;
- }
-
- MSG msg;
- while( GetMessage( &msg, NULL, 0, 0 ) ){
- TranslateMessage( &msg ); // Translates virtual key codes.
- DispatchMessage( &msg ); // Dispatches message to window.
- }
- ExitInstance();
- return msg.wParam; // Returns the value from PostQuitMessage.
- }
-
- BOOL InitInstance( HINSTANCE hInstance )
- {
- // SetLayeredWindowAttributes のエクスポートアドレスの取得
- hInst = LoadLibrary( "USER32.DLL" );
- if( hInst ){
- _SetLayeredWindowAttributes = (BOOL(WINAPI*)(HWND,COLORREF,BYTE,DWORD))
- GetProcAddress( hInst, "SetLayeredWindowAttributes" );
- }
- if( _SetLayeredWindowAttributes==NULL ) return FALSE;
-
- playeredWnd = new LayeredWnd( hInstance );
- HWND hWnd = playeredWnd->Create();
- return hWnd?TRUE:FALSE;
- }
-
- void ExitInstance()
- {
- if( playeredWnd ) delete playeredWnd;
- if( hInst ) FreeLibrary( hInst );
- }
-
- LONG APIENTRY MainWndProc( HWND hWnd, UINT message, UINT wParam, LONG lParam )
- {
- if( message==WM_NCCREATE ){
- CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;
- SetWindowLong( hWnd, GWL_USERDATA, (LONG)pcs->lpCreateParams );
- }
- LayeredWnd *pWnd = (LayeredWnd*)GetWindowLong( hWnd, GWL_USERDATA );
- if( pWnd==NULL ){
- return DefWindowProc( hWnd, message, wParam, lParam );
- } else {
- return pWnd->WndProc( hWnd, message, wParam, lParam );
- }
- return 0;
- }
-
- //////////////////////////////////////////////////////////////////////
- // LayeredWnd クラス
- //////////////////////////////////////////////////////////////////////
-
- //////////////////////////////////////////////////////////////////////
- // 構築/消滅
- //////////////////////////////////////////////////////////////////////
-
- LayeredWnd::LayeredWnd( HINSTANCE hInstance )
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof( WNDCLASSEX );
- wc.style = 0;
- wc.lpfnWndProc = (WNDPROC)MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = szAppName;
- wc.hIconSm = NULL;
- RegisterClassEx( &wc );
- m_hInstance = hInstance;
- }
-
- LayeredWnd::~LayeredWnd()
- {
-
- }
-
- HWND LayeredWnd::Create()
- {
- CREATESTRUCT cs;
- cs.lpCreateParams = this;
- cs.hInstance = m_hInstance;
- cs.hMenu = NULL;
- cs.hwndParent = NULL;
- cs.cy = CW_USEDEFAULT;
- cs.cx = CW_USEDEFAULT;
- cs.y = CW_USEDEFAULT;
- cs.x = CW_USEDEFAULT;
- cs.style = WS_OVERLAPPEDWINDOW|WS_VISIBLE;
- cs.lpszName = szTitle;
- cs.lpszClass = szAppName;
- cs.dwExStyle = WS_EX_LAYERED; // レイヤードウィンドウ
- m_hWnd = CreateWindowEx( cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.style,
- cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams );
- return m_hWnd;
- }
-
- LRESULT LayeredWnd::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
- {
- switch( message ){
- case WM_CREATE:
- OnCreate( hWnd );
- return 0;
- case WM_DESTROY:
- OnDestroy();
- PostQuitMessage( 0 );
- return 0;
- }
- return DefWindowProc( hWnd, message, wParam, lParam );
- }
-
- void LayeredWnd::OnCreate( HWND hWnd )
- {
- _SetLayeredWindowAttributes( hWnd, 0, 128, LWA_ALPHA );
- }
-
- void LayeredWnd::OnDestroy()
- {
- }
-